/**
 * 
 */
package wangjg.commons.bean;


/**
 * @author wangjg
 *
 */
public class Converter {
	
    public Object convert(Class<?> type, Object value){

    	if(Number.class.isAssignableFrom(type)){
    		value = this.convertNumber(type, value);
    	}

    	return value;
    }
    
    private Number convertNumber(Class<?> type, Object value){
    	Number result = null;
    	Class<?> srcType = value.getClass();
		if(Number.class.isAssignableFrom(srcType)){
			Number number = (Number)value;
			if(type.equals(Byte.class)||type.equals(byte.class)){
				result = number.byteValue();
			}else if(type.equals(Short.class)||type.equals(short.class)){
				result = number.shortValue();
			}else if(type.equals(Integer.class)||type.equals(int.class)){
				result = number.intValue();
			}else if(type.equals(Long.class)||type.equals(long.class)){
				result = number.longValue();
			}else if(type.equals(Float.class)||type.equals(float.class)){
				result = number.floatValue();
			}else if(type.equals(Double.class)||type.equals(double.class)){
				result = number.doubleValue();
			}
		}else if(String.class.equals(srcType)){
			Double d = Double.valueOf((String)value);
			result = convertNumber(type, d);
		}
    	return result;
    }
    
}